/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread;

import InterfaceClient.JDriveCombo;
import Packets.pkValue;
import StaticClass.ArrSocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;

/**
 *
 * @author Oho
 */
public class ThreadFile extends Thread {

    Socket clientSocket;

    ThreadFile(Socket s) {
        //try {
     //   ArrSocket.ArrFile.add(this);
        clientSocket = s;


    }
    //JDriveCombo cbb =null;

    public void ThreadRead() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
        if (inp == null) {
            inp = new ObjectInputStream(clientSocket.getInputStream());
        }
        //pkValue pk = (pkValue) inp.readObject();


        pkValue vl = (pkValue) inp.readObject();
        int filesize = 6022386; // filesize temporary hardcoded
        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        FileOutputStream fos = new FileOutputStream("");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] mybytearray = (byte[]) vl.getObj();
        bos.write(mybytearray, 0, mybytearray.length);
        bos.flush();
        long end = System.currentTimeMillis();
    }

    @Override
    public void run() {
        ArrSocket.ArrFile.add(this);
        while (true) {
            try {
                ThreadRead();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    // ObjectOutputStream outp = null;
    ObjectInputStream inp = null;

    public void ThreadWrite1(String text, String URLClient) throws IOException {
        File f = new File(text);
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
//
        DataInputStream dis = new DataInputStream(bis);


        OutputStream os = clientSocket.getOutputStream();
        //Sending size of file.
        DataOutputStream dos = new DataOutputStream(os);
//        dos.writeUTF(clientSocket+);
                dos.writeUTF(URLClient+"/"+f.getName());
        dos.writeLong(f.length());
        byte[] arr = new byte[1024];
        try {
            int len = 0;
            while ((len = dis.read(arr)) != -1) {
                dos.write(arr, 0, len);

            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        fis.close();
        bis.close();
        dis.close();
        os.flush();
        
        dos.flush();

     //   socket.close();
//        outStream.writeObject("SEND_COMPLETE"); 

        //      bis.read(mybytearray, 0, mybytearray.length);
        // goi ten file 

        // goi duong dan luu file lai client

//        pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(URLClient);
//        outp.writeObject(pk);
        //goi du lieu 
//        pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(mybytearray);
//        outp.writeObject(pk);
        // outp.flush();
    }
//    public void ThreadWrite(String text,String URLClient) throws IOException {
//        if (outp == null) {
//            outp = new ObjectOutputStream(clientSocket.getOutputStream());
//        }
//        File myFile = new File(text);
//        byte[] mybytearray = new byte[(int) myFile.length()];
//        FileInputStream fis = new FileInputStream(myFile);
//        BufferedInputStream bis = new BufferedInputStream(fis);
//        bis.read(mybytearray, 0, mybytearray.length);
//        // goi ten file 
//        pkValue pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(URLClient+myFile.getName());
//        outp.writeObject(pk);
//        // goi duong dan luu file lai client
//        
////        pk = new pkValue();
////        pk.setCommand(2);
////        pk.setValue(1);
////        pk.setObj(URLClient);
////        outp.writeObject(pk);
//        //goi du lieu 
//        pk = new pkValue();
//        pk.setCommand(2);
//        pk.setValue(1);
//        pk.setObj(mybytearray);
//        outp.writeObject(pk);
//        outp.flush();
//    }
}
